package com.smcaiot.jfttask.domain;

import com.smcaiot.core.exception.DomainException;
import com.smcaiot.core.stereotype.DomainService;
import com.smcaiot.jfttask.south.port.TaskBatchRepository;
import com.smcaiot.jfttask.south.port.TaskFlowRepository;
import com.smcaiot.jfttask.south.port.TaskRepository;
import com.smcaiot.jfttask.south.port.UserClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.smcaiot.jfttask.domain.TaskFlowStatus.TASK_LOG_STATUS_CREATE;

/**
 * @Author: cz
 * @Date: 2023/9/27
 * @Description:
 */
@Service
@DomainService
@Slf4j
public class TaskService {
    @Autowired
    private UserClient userClient;
    @Autowired
    private TaskRepository taskRepository;
    @Autowired
    private TaskFlowRepository taskFlowRepository;
    @Autowired
    private TaskBatchRepository taskBatchRepository;

    public void addTask(Task task, TaskBatch taskBatch, String userId) {
        UserReview userReview = userClient.getUser(userId);
        boolean isFJLeader = userClient.isFJLeader(userReview.getRole());
        task.isValidIfAdd(isFJLeader);

        taskRepository.saveTask(task);
        DomainException.testTransaction("新增任务测试事务");

        taskBatch.setTaskBatchId(TaskBatchId.next());
        taskFlowRepository.saveTaskFlow(task.getTaskId(), taskBatch.getTaskBatchId(), TASK_LOG_STATUS_CREATE);

        // TODO
    }
}
